package cn.v2tila.rsosa.management.service.serviceImpl;

import cn.v2tila.rsosa.management.entity.DeliveryEntity;
import cn.v2tila.rsosa.management.jpa.DeliveryJPA;
import cn.v2tila.rsosa.management.service.DeliveryService;
import cn.v2tila.rsosa.management.utils.PageDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author v2tila on 15:00 2019/4/19
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {
    @Autowired
    private DeliveryJPA deliveryJPA;
    @Override
    public PageDataResult getDeliver(int page, int limit, DeliveryEntity deliveryEntity) {
        PageDataResult pageDataResult = new PageDataResult();
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        ExampleMatcher matcher;

        if(deliveryEntity.getAssoId()==0){
            matcher = ExampleMatcher.matching()
//                    .withMatcher("uniqueId", ExampleMatcher.GenericPropertyMatchers.contains())
                    .withIgnorePaths("id","vitaeId","assoId","zpxxId","code");
        }else{
            matcher = ExampleMatcher.matching()
                    .withIgnorePaths("vitaeId","id","zpxxId","code");
        }
        Example<DeliveryEntity> example = Example.of(deliveryEntity,matcher);
        Page<DeliveryEntity> pages = deliveryJPA.findAll(example,pageable);
        List<DeliveryEntity> assoEntityList = pages.getContent();
        int count = deliveryJPA.findAll(example).size();
        pageDataResult.setData(assoEntityList);
        pageDataResult.setCount(count);
        return pageDataResult;
    }
}
